package com.hustzp.com.xichuangzhu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.login.LoginActivity;
import com.hustzp.com.xichuangzhu.me.AddQuoteActivity;
import com.hustzp.com.xichuangzhu.me.CustomQuoteListActivity;
import com.hustzp.com.xichuangzhu.model.Review;
import com.hustzp.com.xichuangzhu.poetry.QuotesAct;
import com.hustzp.com.xichuangzhu.utils.ScreenshotUtil;
import com.hustzp.com.xichuangzhu.widget.FlyBackground;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/25 18:38
 * @Description:
 */
public class ReViewFragment extends Fragment implements View.OnClickListener, FlyBackground.OnLoadColection {

    private RelativeLayout rlytHeadCollectionBg = null;
    private ImageView imagviewConnection = null;
    private TextView tv_back;
    private ImageButton imgAddQuote;
    private ImageButton imgShare;

    private ImageView imageMore;

    private FlyBackground flybg_layout = null;

    private boolean noLiked = false;
    private RelativeLayout rlytBackBg;

    //    private FlyBackground fragReviewBodyBg;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_review, container, false);
        initViews(view);
        if (getArguments() != null) {
            tv_back.setVisibility(View.VISIBLE);
            String from = getArguments().getString("from");
            if (AddQuoteActivity.class.getSimpleName().equals(from)) {
                imagviewConnection.setVisibility(View.GONE);
                imgAddQuote.setVisibility(View.GONE);
                imgShare.setVisibility(View.VISIBLE);
                rlytBackBg.setVisibility(View.VISIBLE);
                tv_back.setText("自由创作");
                noLiked = true;
            } else if (CustomQuoteListActivity.class.getSimpleName().equals(from)) {
                imagviewConnection.setVisibility(View.GONE);
                imgAddQuote.setVisibility(View.GONE);
                imgShare.setVisibility(View.VISIBLE);
                rlytBackBg.setVisibility(View.VISIBLE);
                tv_back.setText("创作记录");

                noLiked = true;
            } else {
                imgAddQuote.setVisibility(View.VISIBLE);
                imgShare.setVisibility(View.GONE);
                rlytBackBg.setVisibility(View.VISIBLE);
                tv_back.setText("返回");
            }
            flybg_layout.setReViewQuotes((Review) getArguments().getSerializable(QuotesAct.class.getSimpleName()));
        } else {
            imgAddQuote.setVisibility(View.VISIBLE);
            imgShare.setVisibility(View.GONE);
            tv_back.setVisibility(View.GONE);
            rlytBackBg.setVisibility(View.GONE);
        }
        return view;
    }

    private void initViews(View view) {
        flybg_layout = (FlyBackground) view.findViewById(R.id.flybg_layout);
        rlytHeadCollectionBg = (RelativeLayout) view.findViewById(R.id.rlytHeadCollectionBg);
        rlytHeadCollectionBg.setTag(null);
        rlytHeadCollectionBg.setOnClickListener(this);
        imagviewConnection = (ImageView) view.findViewById(R.id.imagviewConnection);
        imagviewConnection.setOnClickListener(this);
        imgAddQuote = (ImageButton) view.findViewById(R.id.imgAddQuote);
        imgAddQuote.setOnClickListener(this);
        imgShare = (ImageButton) view.findViewById(R.id.imgShare);
        imgShare.setOnClickListener(this);
        rlytBackBg = (RelativeLayout) view.findViewById(R.id.rlytBackBg);
        imageMore = (ImageView) view.findViewById(R.id.img_more);
        imageMore.setOnClickListener(this);

        tv_back = (TextView) view.findViewById(R.id.tv_back);
        rlytBackBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        //加载收藏
        flybg_layout.setOnLoadColection(this);

    }

    @Override
    public void onClick(View v) {
        final XCZBaseFragmentActivity mainAct = (XCZBaseFragmentActivity) getActivity();
        switch (v.getId()) {
            case R.id.rlytHeadCollectionBg:
                break;
            case R.id.imgShare:
                mainAct.screenShotView = flybg_layout.getTopView();
                mainAct.initSharePopupWindow();
                mainAct.showSharePopWindow();
                break;
            case R.id.img_more:

                List<String> menus = new ArrayList<>();
                if (!noLiked) {
                    menus.add("分享");
                }
                menus.add(getString(R.string.baocunweitupian));

                mainAct.initSharePopupWindow();
                mainAct.showSharePopupWindow(menus, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        if (noLiked) {
                            i = 1; // goto 保存图片
                        }

                        switch (i) {
                            case 0:
                                // 分享
                                mainAct.screenShotView = flybg_layout.getTopView();
                                mainAct.isAdd = false;
                                mainAct.showSharePopupWindow();
                                break;
                            case 1:
                                // 保存图片
                                ScreenshotUtil.saveBitmapView(getContext(), flybg_layout.getTopView(), false);
                                mainAct.hideAlert();
                                break;
                        }

                    }
                });

                break;

            case R.id.imagviewConnection:

                if (AVUser.getCurrentUser() == null) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                AVObject object = (AVObject) rlytHeadCollectionBg.getTag();
                if (object == null) {
                    AVObject likeQuote = new AVObject("LikeQuote");
                    likeQuote.put("user", AVUser.getCurrentUser());
                    likeQuote.put("quoteId", flybg_layout.getTopReview().getId());
                    likeQuote.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {
                                //保存成功, 重新加载收藏
                                Review review = new Review();
                                review.setId(flybg_layout.getTopReview().getId());
                                loadCollection(review);
                            }
                        }
                    });

                } else {
                    object.deleteInBackground(new DeleteCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {
                                //删除成功
                                rlytHeadCollectionBg.setTag(null);
                                imagviewConnection.setImageDrawable(getResources().getDrawable(R.mipmap.frag_review_head_collection_default));
                            }
                        }
                    });
                }
                break;
            case R.id.imgAddQuote:
                Intent intent = new Intent(getActivity(), AddQuoteActivity.class);
                intent.putExtra("from", ReViewFragment.class.getSimpleName());
                startActivity(intent);
                break;
        }

    }

    @Override
    public void loadCollection(Review review) {
        //没登录,不显示收藏按钮
        if (AVUser.getCurrentUser() == null) {
            return;
        }
        AVObject user = AVObject.createWithoutData("objectId", AVUser.getCurrentUser().getObjectId());
        AVQuery<AVObject> query1 = new AVQuery<>("LikeQuote");
        query1.whereEqualTo("user", user);

        AVQuery<AVObject> query = query1;
        // 自由创作、创作记录，还没有收藏操作
        if (!noLiked) {
            AVQuery<AVObject> query2 = new AVQuery<>("LikeQuote");
            query2.whereEqualTo("quoteId", Integer.valueOf(review.getId()));

            query = AVQuery.and(Arrays.asList(query1, query2));
        }


        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (list != null && !list.isEmpty()) {
                    rlytHeadCollectionBg.setTag(list.get(0));
                    imagviewConnection.setImageDrawable(getResources().getDrawable(R.mipmap.frag_review_head_collection_pressed));
                } else {
                    imagviewConnection.setImageDrawable(getResources().getDrawable(R.mipmap.frag_review_head_collection_default));
                    rlytHeadCollectionBg.setTag(null);
                }
            }
        });
    }
}
